﻿using System;
using System.Diagnostics;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace Winz
{
    class Settings
    {
        public Settings()
        {
            Accounts = new List<string>();
            wowPath = "";
            string settingsFolder = Path.GetDirectoryName(SettingsPath);
            if (!Directory.Exists(settingsFolder))
                Directory.CreateDirectory(settingsFolder);
        }

        public string SettingsPath
        {
            get
            {
                return Path.Combine(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName),
                       "Setting.xml");
            }
        }

        public string wowPath { get; set; }
        public List<string> Accounts { get; set; }




        public void Save()
        {
            try
            {
                var root = new XElement("FileCorrecter");
                root.Add(new XElement("wowPath", wowPath));
                var accountsElement = new XElement("Accounts");
                foreach (string account in Accounts)
                {
                    var accountElement = new XElement("Account");
                    accountElement.Add(new XElement("Name", account));
                    accountsElement.Add(accountElement);
                }
                root.Add(accountsElement);
                var xmlSettings = new XmlWriterSettings
                      {
                          OmitXmlDeclaration = true,
                          Indent = true,
                      };

                using (XmlWriter xmlOutFile = XmlWriter.Create(SettingsPath, xmlSettings))
                {
                    root.Save(xmlOutFile);
                }
            }
            catch (Exception ex)
            {
                //Log.Err(ex.ToString());
            }
        }

        public static Settings Load()
        {
            var settings = new Settings();
            try
            {
                if (File.Exists(settings.SettingsPath))
                {
                    XElement root = XElement.Load(settings.SettingsPath);
                    settings.wowPath = root.Element("wowPath").Value;
                    XElement AccountsElement = root.Element("Accounts");
                    foreach (XElement accountElement in AccountsElement.Elements("Account"))
                    {
                        string name = accountElement.Element("Name").Value;
                        settings.Accounts.Add(name);
                    }
                }
                else 
                {
                    if(Directory.Exists( @"C:\WOW"))
                        settings.wowPath = @"C:\WOW";
                    else if(Directory.Exists( @"E:\WOW"))
                        settings.wowPath = @"E:\WOW";
                    settings.Accounts.Add("fake account");
                    settings.Save();
                }
            }
            catch (Exception ex)
            {
            }
            return settings;
        }
    }
}

